﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using FrmMain;
using System.IO;


namespace medio
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bindText();
            bindFlp();
            bindAx();
            this.MouseWheel += Mouse_Wheel;
            
           
        }

        private void Mouse_Wheel(object sender,MouseEventArgs e) {
          int volume=  this.axShow.settings.volume;
            if (e.Delta > 0) {
                volume++;
                if (volume > 100) {
                    volume = 100;
                }
            }
            else{
                volume--;
                if (volume < 0) {
                    volume = 0;
                }
            }
            this.axShow.settings.volume=volume;
        }
        /// <summary>
        /// 获取数据路径
        /// </summary>
        private void bindText()
        {
        string str=  service.getSongUrl();
            this.textBox1.Text = str;

        }

        /// <summary>
        /// 自动播放
        /// </summary>
        private void bindAx()
        {
            this.axShow.URL = textBox1.Text + "\\"+ songlist[0].SongUrl;
            MessageBox.Show(textBox1.Text);
        }

        private Dao dao = new Dao();

        /// <summary>
        /// 歌曲集合
        /// </summary>
        private List<songEntity> songlist=new   List<songEntity>();
        Service service = new Service();

        
        
        /// <summary>
        /// 绑定歌曲列表
        /// </summary>
        private void bindFlp()
        {
            songlist= service.GetSongList();
            for (int i = 0; i < songlist.Count; i++)
            {
                Label txt = new Label();
                txt.Text = songlist[i].Ab;
                txt.Margin = new Padding(10, 10,10,10);
                txt.Size = new Size(130, 20);
                txt.Tag = i;
                txt.Name = "" + i;
                txt.Click += Lable_Click;
                this.flpShow.Controls.Add(txt);
            }
        }
        /// <summary>
        /// 播放下标
        /// </summary>
        private int index;
        /// <summary>
        /// 歌曲姓名单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Lable_Click(object sender, EventArgs e) {
            Label label = (Label)sender;
            index = Convert.ToInt32(label.Tag);
            songEntity entity =songlist[index];
            this.axShow.URL=textBox1.Text+"\\"+ entity.SongUrl;
            
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            nextSong();
            //this.flpShow.Controls.RemoveByKey((index-1) + "");
        }

        private void nextSong()
        {
            if (index == songlist.Count - 1)
            {//最后一首
                index = 0;
            }
            else
            {
                index++;
            }

            tmChange.Enabled = true;
        }

        private void tmChange_Tick(object sender, EventArgs e)
        {
            Timer time = (Timer)sender;
            time.Enabled = false;
            songEntity entity = songlist[index];
            this.axShow.URL = textBox1.Text+"\\"+ entity.SongUrl;
            MessageBox.Show(axShow.URL);
        }

        private void btnOld_Click(object sender, EventArgs e)
        {
            if (index == 0) {
                index = songlist.Count - 1;
            }
            else{
                index--;
            }
            tmChange.Enabled = true;
           // this.flpShow.Controls.RemoveByKey(index + "");
        }

        private void axShow_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if (axShow.playState == WMPLib.WMPPlayState.wmppsStopped) {
                nextSong();
                //this.flpShow.Controls.RemoveByKey(index + "");
            }
        }

        private void btnLald_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog of = new FolderBrowserDialog();
             
            DialogResult result=   of.ShowDialog();
            if (result == DialogResult.OK) {
                textBox2.Text = of.SelectedPath;
            }
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            IEnumerable<string> enums= Directory.EnumerateFiles(textBox1.Text);
            try
            {
                foreach (var item in enums)
                {
                    string str = item.Substring(item.LastIndexOf("\\"));
                    File.Move(item, textBox2.Text+str);
                }
                if (service.setUrl(textBox2.Text))
                {
                    MessageBox.Show("移动成功");
                     bindText();
                }
                else {

                    MessageBox.Show("移动失败");
                }

               
            }
            catch (Exception en)
            {
                MessageBox.Show(en.Message);
            }
            

        }

       
    }

  
}
